<script setup>
import { getBizAccountInfo, addBizAccountInfo, updateBizAccountInfo, findMaxSort } from "@/api/biz/bizAccountInfo"

const props = defineProps({
  id: {
    type: [Number, String],
    default: ""
  }
});

const formRef = ref();
const data = ref({
  loading: false,
  form: {
    id: props.id
  },
  rules: {
    name: [
      { required: true, message: "请输入登陆账号", trigger: "blur" },
      { validator: getcheckUserNameUnique }
    ]
  }
});

onMounted(() => {
  if (data.value.form.id != "") {
    getInfo();
  }
});

function getInfo() {
  data.value.loading = true;
  getBizAccountInfo(props.id).then((res) => {
    data.value.loading = false;
    data.value.form = res.data;
  });
}

function getcheckUserNameUnique(rule, value, callback) {
if (data.value.form.id !== "" || data.value.form.id !== undefined) {
    callback();
  }
  const msg = "登陆账号已存在";
  if (value === "") {
    callback();
  } else {
    const checkData = {
      userName: value
    };
    checkUserNameUnique(checkData).then((response) => {
      if (response.data.code === "1") {
        callback();
      } else {
        callback(msg);
      }
    });
  }
}
defineExpose({
  submit(callback) {
    if (data.value.form.id == "") {
      formRef.value.validate((valid) => {
        if (valid) {
          addBizAccountInfo(data.value.form).then(() => {
            ElMessage.success({
              message: "保存成功",
              center: true
            });
            callback && callback();
          });
        }
      });
    } else {
      formRef.value.validate((valid) => {
        if (valid) {
          updateBizAccountInfo(data.value.form).then(() => {
            ElMessage.success({
              message: "修改成功",
              center: true
            });
            callback && callback();
          });
        }
      });
    }
  }
});
</script>

<template>
  <div v-loading="data.loading">
    <el-form
      ref="formRef"
      :model="data.form"
      :rules="data.rules"
      label-width="120px"
      label-suffix=" "
      label-position="top"
      style="padding: 0 20px"
    >
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="姓名" prop="name">
           <el-input placeholder="请输入姓名" v-model="name" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
           <el-form-item label="姓名" prop="name">
             <el-input placeholder="请输入姓名" v-model="name" />
           </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
